package org.apache.solr.util;

import com.google.common.collect.ForwardingMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Wraps another map, keeping track of each key that was seen via {@link #get(Object)} or {@link #remove(Object)}.
 */
@SuppressWarnings("unchecked")
public class MapListener<K, V> extends ForwardingMap<K, V> {

    private final Map<K, V> target;
    private final Set<K> seenKeys;

    public MapListener(Map<K, V> target) {
        this.target = target;
        seenKeys = new HashSet<>(target.size());
    }

    public Set<K> getSeenKeys() {
        return seenKeys;
    }

    @Override
    public V get(Object key) {
        seenKeys.add((K) key);
        return super.get(key);
    }

    @Override
    public V remove(Object key) {
        seenKeys.add((K) key);
        return super.remove(key);
    }

    @Override
    protected Map<K, V> delegate() {
        return target;
    }
}
